//
//  BarButtonItem.m
//  Test
//
//  Created by ME on 15/8/19.
//  Copyright (c) 2015年 ME. All rights reserved.
//

#import "BarButtonItem.h"

@interface BarButtonItem ()


@end

@implementation BarButtonItem
@synthesize button;

+ (id)itemWithTitle:(NSString *)title selectedTitle:(NSString *)selectedTitle
{
    BarButtonItem *item = [[BarButtonItem alloc] init];
    item.button = [UIButton buttonWithType:UIButtonTypeCustom];
    item.title = title;
    item.selectedTitle = selectedTitle;
    item.button.titleLabel.font  = UIFontRegular(14);
    [item.button setTitleColor:UIColorWithRGBHex(0x00B578) forState:UIControlStateNormal];
    [item.button setTitleColor:UIColorWithRGBHex(0x00B578) forState:UIControlStateHighlighted];
    [item.button setTitleColor:UIColorWithRGBHex(0x00B578) forState:UIControlStateSelected];
    return item;
}

+ (id)itemWithImage:(NSString *)imageName selectedImage:(NSString *)selectedImage
{
    BarButtonItem *item = [[BarButtonItem alloc] init];
    item.button = [UIButton buttonWithType:UIButtonTypeCustom];
    item.image = imageName;
    item.selectedImage = selectedImage;
    return item;
}

#pragma mark - Setter

- (void)setTitle:(NSString *)title
{
    if (_title != title) {
        _title = title;
        [button setTitle:_title forState:UIControlStateNormal];
    }
}

- (void)setSelectedTitle:(NSString *)selectedTitle
{
    if (_selectedTitle != selectedTitle) {
        _selectedTitle = selectedTitle;
        [button setTitle:_selectedTitle forState:UIControlStateSelected];
    }
}

- (void)setImage:(NSString *)image
{
    if (_image != image) {
        _image = image;
        [button setImage:[UIImage imageNamed:_image] forState:UIControlStateNormal];
    }
}

- (void)setSelectedImage:(NSString *)selectedImage
{
    if (_selectedImage != selectedImage) {
        _selectedImage = selectedImage;
        [button setImage:[UIImage imageNamed:_selectedImage] forState:UIControlStateSelected];
    }
}

- (void)setTarget:(id)target
{
    if (_target != target) {
        _target = target;
    }
}

- (void)setSelector:(SEL)selector
{
    if (_selector != selector) {
        _selector = selector;
        [button removeTarget:_target action:NULL forControlEvents:UIControlEventTouchUpInside];
        [button addTarget:_target action:selector forControlEvents:UIControlEventTouchUpInside];;
    }
}

// 调节位置
- (void)setTitleInsets:(UIEdgeInsets)titleInsets
{
    if (UIEdgeInsetsEqualToEdgeInsets(_titleInsets, titleInsets)) {
        _titleInsets = titleInsets;
        button.titleEdgeInsets = titleInsets;
    }
}

- (void)setImageInsets:(UIEdgeInsets)imageInsets
{
    if (UIEdgeInsetsEqualToEdgeInsets(_imageInsets, imageInsets)) {
        _imageInsets = imageInsets;
        button.imageEdgeInsets = imageInsets;
    }
}


- (void)setupAutoSizeWithHorizontalPadding:(CGFloat)hPadding buttonHeight:(CGFloat)buttonHeight
{
    if (button.titleLabel.text.length > 0) {
        CGSize size = [button.titleLabel.text sizeWithAttributes:@{NSFontAttributeName : button.titleLabel.font}];
        button.width = size.width + 2 * hPadding;
    }else if (button.imageView.image){
        button.width = button.imageView.image.size.width + 2 * hPadding;
    }
    button.height = buttonHeight;
}
@end
